import { _decorator, Component, Node, geometry, CameraComponent, SystemEventType, GraphicsComponent, color, Color, PhysicsRayResult, PhysicsSystem, SystemEvent, systemEvent, EventTouch, EventType, v3, Vec3, Quat, Vec2, CanvasComponent, TERRAIN_HEIGHT_BASE, ModelComponent, Mesh, director, RigidBodyComponent, quat, math } from "cc";
const { ccclass, property } = _decorator;
import { BallSystem } from '../system/ballSystem';
@ccclass("gunShoot")
export class gunShoot extends Component {

    @property(CameraComponent)
    camera: CameraComponent | null = null

    @property(Node)
    canvas: Node | null = null

    @property(Node)
    graphics: Node | null = null

    @property(Node)
    line: Node | null = null

    @property
    ShootSpeed: number = 300;

    @property
    rotateSpeed: number = 5

    RotaValue = 0
    RotateMax = 90
    RotateMin = -90

    ray: geometry.ray

    onLoad() {

    }

    start() {
        console.log('start');
        systemEvent.on(SystemEventType.TOUCH_START, this.onTouchStart, this)
        systemEvent.on(SystemEventType.TOUCH_MOVE, this.onTouchMove, this)
        systemEvent.on(SystemEventType.TOUCH_END, this.onTouchEnd, this)
        this.ray = new geometry.ray(0, 0.5, 15, 0, 0.5, -15)

    }

    update(deltaTime: number) {
        //有用        

        if (PhysicsSystem.instance.raycastClosest(this.ray)) {
            const r = PhysicsSystem.instance.raycastClosestResult;
        }
    }

    onTouchStart(touch, event) {
        // console.log("onTouchStart")

        //取distance的中心点，并拓展scale


    }

    onTouchMove(touch, event) {
        // console.log("onTouchStart")

        this.gunRotate(touch, event)
        this.lineTip(touch, event)
    }

    onTouchEnd(touch, event) {
        geometry.ray.fromPoints(this.ray, Vec3.ZERO, Vec3.ZERO)
        this.gunRotate(touch, event)
        this.fire(touch, event)
    }


    gunRotate(touch, event) {
        // console.log(touch, event);
        //获取当前触碰点的世界坐标
        let currentPos: Vec2 = touch.getLocation();
        // console.log("当前触碰点的世界坐标是：" + currentPos);
        //获取当前触碰点的UI坐标
        let currTouchPos: Vec2 = touch.getUILocation();
        // console.log("当前触碰点的UI坐标是：" + currTouchPos);

        //获取旋转的方向
        let pos: Vec2 = touch.getUIDelta();
        // console.log("获取触点距离上一次事件移动的距离对象：" + pos);
        //每次touch都加一个角度
        if (pos.x < 0)//向左
        {
            this.RotaValue -= this.rotateSpeed;
        }
        else //向右
        {
            this.RotaValue += this.rotateSpeed;
        }

        //旋转目标物体
        this.node.eulerAngles = new Vec3(0, -this.RotaValue, 0);
        // console.log("当前目标物体的欧拉角：" + this.node.eulerAngles);

        if (this.node.eulerAngles.y > 90 || this.node.eulerAngles.y < -90) {
            if (this.node.eulerAngles.y < 0) {
                this.node.eulerAngles = new Vec3(0, -90, 0);
            } else {
                this.node.eulerAngles = new Vec3(0, 90, 0);
            }
        }

    }

    lineTip(touch, event) {
        let startPos = this.line.getWorldPosition()
        let dir = this.line.forward
        // console.log(startPos, '\n', dir);
        geometry.ray.fromPoints(this.ray, startPos, dir)

    }


    fire(touch, event) {
        let obj = (window['BallSystem'] as BallSystem).addBall(this.randomBallNum()) as Node
        obj.parent = (window['BallSystem'] as BallSystem).ballLayout
        obj.setPosition(cc.v3(0, 0, 0))
        obj.eulerAngles = this.node.eulerAngles

        let back: Vec3 = cc.v3(-obj.forward.x * this.ShootSpeed, 0, -obj.forward.z * this.ShootSpeed)
        obj.getComponent(RigidBodyComponent).applyImpulse(back)
    }


    randomBallNum(): number {
        let exponent = Math.floor(Math.random() * 11 + 1)
        return Math.pow(2, exponent)
    }
}




// TouchEnd(event) {
//     this.RotaValue = 0;
// }